package per.ethan.ssm.controller;

import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import per.ethan.ssm.domain.Page;
import per.ethan.ssm.domain.ResultBean;
import per.ethan.ssm.entity.User;
import per.ethan.ssm.service.UserService;

import java.util.List;
import java.util.Map;

/**
* @author Ethan
* @date 2020/03/11 16:15
*/
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class UserController{

    @Qualifier("userService")
    private final UserService userService;

    @PostMapping
    @ApiOperation(value = "保存实体信息")
    public ResultBean<Long> saveUser(@RequestBody User user){
        return ResultBean.success(userService.save(user));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过id查询信息")
    public ResultBean<User> findById(@PathVariable Long id){
        return ResultBean.success(userService.findById(id));
    }

    @PostMapping("/page")
    @ApiOperation(value = "分页查询")
    public ResultBean<Page<User>> findByPage(@RequestBody Page<User> page) {
        return ResultBean.success(userService.findByPage(page));
    }

    @GetMapping("/getByWhere")
    @ApiOperation(value = "按条件查询信息")
    public ResultBean<List<User>> findByWhere(@RequestParam Map<String, Object> paramMap) {
        return ResultBean.success(userService.findByMap(paramMap));
    }

    @PutMapping
    @ApiOperation(value = "修改实体信息")
    public ResultBean<Boolean> modify(@RequestBody User user) {
        return ResultBean.success(userService.update(user));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除实体信息")
    public ResultBean<Boolean> removeById(@PathVariable Long id){
        return ResultBean.success(userService.logicDeleteById(id));
    }

    @DeleteMapping("/batchRemove")
    @ApiOperation(value = "批量删除实体信息")
    public ResultBean<Boolean> batchRemove(@RequestBody List<Long> idList){
        return ResultBean.success(userService.logicBatchDelete(idList));
    }

}
